<?php

class MessageController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;
	protected $_dbAdapter;


	public function init() {

		$this->commonAction();	

	}
	//用户首页
	public function indexAction() {
		try {
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	 
	
	//查询学生列表	
	 public function studentAction() {
		try {  	
				  
		    $where='where ismainid=1 and inactive=0 ';	
			 
		   $groupid = ( int ) $this->_request->getParam ( 'groups' );
		   $school_id = $this->_request->getParam ( 'school' ); 		
	       $classid  = $this->_request->getParam ( 'class' ); 
		   $cnname  = $this->_request->getParam ( 'cnname' ); 
		  
		  $this->view->groups=$groupid; 
		  $this->view->school_id=$school_id;  
		  $this->view->classid=$classid; 
		  $this->view->cnname=$cnname; 
		         
		  
				if($school_id){ 
					  $schoolarray=explode('_',$school_id);  
					  $schoolid=$schoolarray['0'];
				  }
				   
				 if($groupid){
					  $where .=' and  groupid="'.$groupid.'" ';
					 $this->view->groupid= $groupid; 
					 }
					 
				 if($schoolid){
					$where .=' and  schoolid="'.$schoolid.'" '; 
					$this->view->schoolid= $schoolid; 
					} 
					
				 if($classid){
					$where .=' and  classid="'.$classid.'" ';
					  $this->view->classid= $classid; 
					 }	
					 
				 if($cnname){
					$where.=' and cnname="'.$cnname.'" ';
					$this->view->cnname= $cnname; 				 
			       } 
			    

			    $sql="select * from group_student_class_parents_v ".$where; 
			    
		        $result=$this->_dbAdapter->fetchAll($sql);
				
			   
			   foreach($result as $key => $value){
			 	  $result[$key]['password']= $this->authcode($value['password'], 'DECODE', 'mactop') ;
		       }

			   $this->view->result= $result;

			 
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning',3 );
		}
	}
 
   	  public function addAction() {
		try { 
	      
		   if (! $this->isPost ()) {
				throw new Exception ( '参数错误！' );
			  }			
		  $p =$this->_request->getPost (); 

		  $uidlist=array();
		  foreach($p['stu_id'] as $key => $value){
				$p['mobile'][$key]=trim($p['mobile'][$key]);
				$p['cnname'][$key]=trim($p['cnname'][$key]);
			  if(empty($p['mobile'][$key]) || empty($p['cnname'][$key])){ 
				  continue;
			   }

		 //插入 parents   
		 $p['realname'][$key]=trim($p['realname'][$key]);
		 if( strlen($p['realname'][$key])>0){ 

			  if($p['pid'][$key]>0){
					//如果账号存在
				 //  $sql="REPLACE INTO `parents` (`pid`,`realname`,`mobile`) VALUES ('".$p['pid'][$key]."','".$p['realname'][$key]."','".$p['mobile'][$key]."') ";    
				   $result=$this->_dbAdapter->update('parents',array('realname'=>$p['realname'][$key],'mobile'=>$p['mobile'][$key]),'pid='.$p['pid'][$key]);
				   $pid=$p['pid'][$key];
				   
			  }/*else{
				 				//生成账号信息 
				do{
					$username=rand(100000,999999); 
					$password= rand(100000,999999);	
					
					$sql="select pid from parents where username='".$username."' ";
					$pnum = $this->_dbAdapter->fetchOne ($sql);  
					if($pnum){
						//需要重新生成 账号
						$i=1;
						}else{
						  $i=0;	
							}
				 }while($i=0);
				
				 $password =$this->authcode($password,'','mactop');   
				  
			 //    $sql="insert INTO `parents` (`realname`,`mobile`,`username`,`password`) VALUES ('".$p['realname'][$key]."','".$p['mobile'][$key]."','".$username."','".$password."') "; 	
				 $array=array(
				 	'realname'=>$p['realname'][$key],
				 	'mobile'=>$p['mobile'][$key],
				 	'username'=>$username,
				 	'password'=>$password,
				 );
				   $presult=$this->_dbAdapter->insert('parents',$array);    
				   $pid=$this->_dbAdapter->lastInsertId();
			  }*/
			 
		  }	//update parents


			//插入中央的student表
		  if($p['stu_uid'][$key]){
		//	$sql="REPLACE INTO `student` (`uid`,`cnname`,`nikename`,`birthday`,`sex`,`pid`,`groupid`) VALUES (".$p['stu_uid'][$key].",'".$p['cnname'][$key]."','".$p['nikename'][$key]."','".$p['birthday'][$key]."','".$p['sex'][$key]."','".$pid."','".$p['groupid']."') "; 
		 	//update
			$array=array(
				'cnname'=>$p['cnname'][$key],
				'enname'=>$p['enname'][$key],
				'nikename'=>$p['nikename'][$key],
				'birthday'=>$p['birthday'][$key],
				'sex'=>$p['sex'][$key],
				//'pid'=>$pid,
				'mobile'=>$p['mobile'][$key],
				//'studentno'=>$p['stu_id'][$key],
				//'groupid'=>$p['groupid'],
			);
			
			$sresult=$this->_dbAdapter->update('student',$array,'uid='.$p['stu_uid'][$key]);  
			$uid=$p['stu_uid'][$key];
			 
		  }/*else{
			 //insert
//$sql="insert INTO `student` (`cnname`,`nikename`,`birthday`,`sex`,`pid`,`groupid`) VALUES ('".$p['cnname'][$key]."','".$p['nikename'][$key]."','".$p['birthday'][$key]."','".$p['sex'][$key]."','".$pid."','".$p['groupid']."') "; 			 
			  
			$array=array(
				'cnname'=>$p['cnname'][$key],
				'nikename'=>$p['nikename'][$key],
				'birthday'=>$p['birthday'][$key],
				'sex'=>$p['sex'][$key],
				'pid'=>$pid,
				'groupid'=>$p['groupid'],
			);
			$sresult=$this->_dbAdapter->insert('student',$array);
			$uid=$this->_dbAdapter->lastInsertId();
		  }*/
		  
/*		  //插入运营库中的student表 
							$student_y=array(); 
							$student_y['uid']=$uid;  
							$student_y['studentno']=$uid;  
							$student_y['schoolid']=$p['schoolid'];   
							$student_y['classid']=$p['classid']; 
							$student_y['class_uid']=$p['class_uid']; 
							$student_y['cnname']=$p['cnname'][$key]; 
							$student_y['enname']=$p['enname'][$key];  
							$result= $this->_dbAdapter->update('student', $student_y, 'studentid='.$value); 
*/				    
						//$uidlist[]=$uid;
			 
			//  $sql="REPLACE INTO `student_class` (`uid_student`,`uid_class`) VALUES (".$suid.",".$p['class_uid'].") ";	           //   $bind=$this->_dbAdapter->query($sql);
			
	
     }//foreache ---end
	 
/*	   $sql="select uid,studentid from student where uid>0 and class_uid='".$p['class_uid']."'";
	  // echo $sql;  
	   $student_uid_id_list=$this->_dbAdapter->fetchAll($sql);	 
		// print_r($student_uid_id_list);  exit;
		
      // echo "aaaaaaaaaa";	
		//exit;
	 
	 	///绑定学生
	   	$sql='delete from student_class where uid_class='.$p['class_uid'];
		$result = $this->_dbAdapter->query ($sql);  
		$sql='insert into student_class (uid_student,uid_class) select uid,'.$p['class_uid'].' from student where uid in ('.implode(',',$uidlist).')';
		$result = $this->_dbAdapter->query ($sql);  
		
	  
		if($student_uid_id_list){
		
			foreach($student_uid_id_list as $key => $value){
				$this->_dbAdapter->query ('update student_class set studentid='.$value['studentid'] .' where uid_student='.$value['uid'] .' and uid_class='.$p['class_uid']);  
			}
		}
*/			
	     if($result){
			    $this->feedback ( '修改成功', '注意', '/'.$this->controller.'/student/groups/'.$p['groupid'].'/school/'.$p['school_id'].'/class/'.$p['classid'], 'tip',0);
			// }else{	
			//	throw new Exception ( '更新失败 ！' ); 
				// $this->feedback ( '修改成功', '注意', '/'.$this->controller.'/student/groups/'.$p['groupid'].'/school/'.$p['school_id'].'/class/'.$p['classid'], 'tip',0 );
			  }
	     
	    } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	} 
	
	//删除操作
	
	
	public function deleteAction() {
		try { 
		 
		 //学生id
	      $sid =  $this->_request->getParam ( 'sid' ); 
		  $this->view->sid=$sid; 
	    //学生的uid
	     $stuid =  $this->_request->getParam ( 'stuid' ); 
		 $this->view->stuid=$stuid; 
	     //班级的uid
		 $clauid =  $this->_request->getParam ( 'clauid' ); 
		 $this->view->clauid=$clauid; 
		 
	    //班级的id 
		 $classid =  $this->_request->getParam ( 'classid' ); 
		 $this->view->cid=$classid; 
		 //groupid
		 $groupid =  $this->_request->getParam ( 'groupid' ); 
		 $this->view->groupid=$groupid; 
		//schoolid
		 $schoolid =  $this->_request->getParam ( 'schoolid' ); 
		 $this->view->schoolid=$schoolid;  
		 //pid
		 $pid =  $this->_request->getParam ( 'pid' ); 
		 $this->view->pid=$pid;  
		if(empty($sid)){
		    throw new Exception ( '参数错误！' );
		  }
		if(empty($stuid)){
		    //throw new Exception ( '参数错误！' );
		  }
		if(empty($clauid)){
		    throw new Exception ( '参数错误！' );
		  }
		if(empty($classid)){
		    throw new Exception ( '参数错误！' );
		  }    
		if(empty($groupid)){
		    throw new Exception ( '参数错误！' );
		  } 
    	if(empty($schoolid)){
		    throw new Exception ( '参数错误！' );
		  }
	   if(empty($pid)){
		   // throw new Exception ( '参数错误！' );
		  }

		 $scdel=$this->_dbAdapter->update('student_class',array('inactive'=>1),'uid_student="'.$stuid.'" and uid_class="'.$clauid.'"');

		 if($scdel){
			 $isok=1;
		 }else{
			 $isok=0; 
		 }

	      if($isok>0){
			    $this->feedback ( '成功', '注意', '/'.$this->controller.'/student/groups/'.$groupid.'/school/'.$schoolid.'/class/'.$classid, 'tip' ,0);
			 }else{	
				throw new Exception ( '删除失败 ！' ); 
			  }
		  
		  
	  } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	} 
	
		
/*增加学生信息*/
   
  public function addstudentAction() {
     try { 
       
	  $groupid =  $this->_request->getParam ( 'groupid' ); 
	  $this->view->groupid=$groupid; 
	  
	  $schoolid =  $this->_request->getParam ( 'schoolid' ); 
	  $this->view->schoolid=$schoolid; 
		 
	  $classid =  $this->_request->getParam ( 'classid' ); 
	  $this->view->classid=$classid; 	 
	  
	  $sql="select * from class_group_v where groupid='".$groupid."' and schoolid='".$schoolid."' and classid='".$classid."'";
	  $result=$this->_dbAdapter->fetchRow($sql);
	  $this->view->result=$result;  
		  
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}  

    

 public function addopAction() {
     try { 
	 
	      if (! $this->isPost ()) {
				throw new Exception ( '参数错误！' );
			  }			
		  $p =$this->_request->getPost ();  
	    // print_r($p);exit;
		  $randcode=$this->randcode($p['groupid']);
	      
		 //$this->_dbAdapter=$this->getDbLink($p['groupid']);
	     //$this->_dbAdapter->query ( 'set names utf8');
		 
		   $sql="select uid from class where classid='".$p['classid']."'";
	       $uid = $this->_dbAdapter->fetchOne ( $sql ); 	
	
		   $data=array(); 
		   $isok=0;
		   $num=$p['num'];
		      
			
			 while ($i<$num) {
				$y=0;
			do{
				$randcode=$this->randcode($p['groupid']) ;
				$sql="select * from student where bindcode='".$randcode."'";
				$result = $this->_dbAdapter->fetchRow ( $sql ); 
				if($result){
					//需要重新生成 账号
					$y=1;
					}else{
					$y=0;   
						}
				}while($y==1);  
                
			   
			   $data['schoolid']=$p['schoolid']; 
			   $data['classid']=$p['classid']; 
			   $data['groupid']=$p['groupid']; 
		       $data['class_uid']=$uid;  
			   $data['adduserid']=$this->_user->id; 
			   $data['addtime']=time();  
			   $data['bindcode']=$randcode;  
			   $res = $this->_dbAdapter->insert ( 'student', $data ); 	
			   $studentid=$this->_dbAdapter->lastInsertId();
			   $result= $this->_dbAdapter->update('student', array('uid'=>$studentid), 'studentid='.$studentid); 
			 $i++;
            }
			 
		  
	      if($res>0){
			 $this->feedback ( '成功', '注意', '/'.$this->controller.'/student/groups/'.$p['groupid'].'/school/'.$p['schoolid'].'/class/'.$p['classid'], 'tip' ,0);  
		  }else{
			throw new Exception ( '操作失败' );	  
		  }
          
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	} 

       //产生随机码
	   public function randcode($groupid,$len=5){
			
			$string = sprintf("%03d", $groupid); 
			//$num=substr(str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789'), 0, 1); 
			
			 $str1 = "0123456789abcdefghijklmnopqrstuvwxyz";   //输出字符集    
			
			$strlen = strlen($str1)-1;  
			for($j=0 ; $j<$len ; $j++){  
					$s = substr($str1,rand(0,$strlen),1);  
					 $string .= $s;   
			}  
		  return $string; 
	   }  
 

   public function groupsAction() {
		try { 
         $sql="select * from  groups ";
	     $result = $this->_dbAdapter->fetchAll ($sql);
		 for($i = 0; $i<count($result); $i++)
		{
	      $str .= "<option value='".$result[$i]['groupid']."'>".$result[$i]['groupname']."</option>";
		}
		  
	    echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//获取所有的校园列表
	  public function schoollistAction() {
		try { 
           if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
		  $p =$this->_request->getPost ();  
		 
		  $sql="SELECT * FROM `groups` where groupid='".$p['id']."' ";
		  $result = $this->_dbAdapter->fetchRow ($sql);
	 
/*			//连接运营库
			$dbArray=array();
			$dbArray['host']=$result['dbhost'];
			$dbArray['username']=$result['dbusername'];
			$dbArray['password']=$this->authcode($result['dbpassword'], 'DECODE', 'mactop') ;
			$dbArray['dbname']=$result['dbname'];
			
			$dbAdapter = Zend_Db::factory ("PDO_MYSQL", $dbArray );
			Zend_Registry::set('dbAdapter', $dbAdapter);

			$dbAdapter=Zend_Registry::get('dbAdapter');
			$dbAdapter->query ( 'set names utf8');	
*/			 
		    $sql="select * from  school where groupid=".$p['id'];   
			$schoollist= $this->_dbAdapter->fetchAll ($sql);
	                 

			 
		 for($i = 0; $i<count($schoollist); $i++)
		{
		  $str .= "<option value='".$schoollist[$i]['schoolid'].'_'.$result['groupid']."'>".$schoollist[$i]['schoolname']."</option>";
		}
		   
	      echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	 
	
	//获取所有的班级信息
	  public function classlistAction() {
		try { 
           if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
		  $p =$this->_request->getPost ();  
		  $array=explode('_',$p['id']);
		  
		  $sql="SELECT * FROM `groups` where groupid='".$array['1']."' ";
		  $result = $this->_dbAdapter->fetchRow ($sql);
	 
/*			//连接运营库
			$dbArray=array();
			$dbArray['host']=$result['dbhost'];
			$dbArray['username']=$result['dbusername'];
			$dbArray['password']=$this->authcode($result['dbpassword'], 'DECODE', 'mactop') ;
			$dbArray['dbname']=$result['dbname'];
			
			$dbAdapter = Zend_Db::factory ("PDO_MYSQL", $dbArray );
			Zend_Registry::set('dbAdapter', $dbAdapter);

			$dbAdapter=Zend_Registry::get('dbAdapter');
			$dbAdapter->query ( 'set names utf8');	
*/			 
		  $sql="select * from  class  where isdel=0 and schoolid='".$array['0']."' ";   
		  $classlist= $this->_dbAdapter->fetchAll ($sql);
	  
			 
		 for($i = 0; $i<count($classlist); $i++)
		{
		  $str .= "<option value='".$classlist[$i]['classid']."'>".$classlist[$i]['classname']."</option>";
		}
		   
	      echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	
	
	
	
	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}}

